package com.hnzq.cloud.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnzq.cloud.entity.FreGoods;
import com.hnzq.cloud.entity.FreGoodsImg;
import com.hnzq.cloud.exception.FreGoodsException;
import com.hnzq.cloud.exception.FreGoodsImgException;
import com.hnzq.cloud.mapper.FreGoodsImgMapper;
import com.hnzq.cloud.param.AddFreGoodsImgParam;
import com.hnzq.cloud.service.FreGoodsImgService;
import com.hnzq.cloud.service.FreGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jams
 * @since 2022-02-06
 */
@Service
public class FreGoodsImgServiceImpl extends ServiceImpl<FreGoodsImgMapper, FreGoodsImg> implements FreGoodsImgService {

    @Resource
    private FreGoodsService freGoodsService;

    @Resource
    private FreGoodsImgService freGoodsImgService;

    @Override
    public Boolean addFreGoodsImgs(AddFreGoodsImgParam addFreGoodsImgParam) {
        FreGoods freGoods = freGoodsService.getById(addFreGoodsImgParam.getGoodsId());
        if (freGoods==null) throw new FreGoodsException("该商品不存在",144);
        List<Integer> imgIds = addFreGoodsImgParam.getImgIds();
        List<FreGoodsImg> freGoodsImgList = new ArrayList<>();
        for (Integer imgId : imgIds) {
            FreGoodsImg freGoodsImg = new FreGoodsImg();
            freGoodsImg.setGoodsId(addFreGoodsImgParam.getGoodsId());
            freGoodsImg.setImgId(imgId);
            freGoodsImgList.add(freGoodsImg);
        }
        return saveBatch(freGoodsImgList);
    }

}
